package vn.vfriends.payment.ws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.payment.jpa.entity.Account;
import vn.vfriends.payment.jpa.entity.Transaction;
import vn.vfriends.payment.service.AccountService;
import vn.vfriends.payment.util.Constants;
import vn.vfriends.payment.ws.exception.WSException;

/**
 *
 * @author tuan@vfriends.vn
 */
@WebService(serviceName = "AccountWS")
public class AccountWS {

    private static final Logger logger = LoggerFactory.getLogger(AccountWS.class);
    @Inject
    private AccountService accountService;
    @Resource
    private WebServiceContext wsContext;

    private boolean authenticate() {
        MessageContext messageContext = wsContext.getMessageContext();
        Map headers = (Map) messageContext.get(MessageContext.HTTP_REQUEST_HEADERS);
        List userList = (List) headers.get(Constants.WS_USERNAME);
        List passList = (List) headers.get(Constants.WS_USERNAME);

        String username = (userList != null) ? userList.get(0).toString() : "";
        String password = (passList != null) ? passList.get(0).toString() : "";

        if ("wsuser".equals(username) && "wsuser".equals(password)) {
            return true;
        } else {
            return false;
        }
    }

    @WebMethod(operationName = "generateCIF")
    public String generateCIF(@WebParam(name = "userId") Long userId, @WebParam(name = "initAmount") Long initAmount)
            throws WSException {
        String cif = "";
        try {
            boolean isAuthenticated = this.authenticate();
            if (!isAuthenticated) {
                logger.info("Authentication failed");
                throw new WSException(WSException.AUTHENTICATION_FAILED, "Authentication failed");
            } else {
                Account account = accountService.generateCIF(userId, initAmount);
                cif = account != null ? account.getCif() : "";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new WSException(WSException.ERROR, e.getMessage());
        }
        return cif;
    }

    public String resetAccount(@WebParam(name = "userId") Long userId, @WebParam(name = "initAmount") Long initAmount)
            throws WSException {
        String cif = "";
        try {
            boolean isAuthenticated = this.authenticate();
            if (!isAuthenticated) {
                logger.info("Authentication failed");
                throw new WSException(WSException.AUTHENTICATION_FAILED, "Authentication failed");
            } else {
                Account account = accountService.resetAccount(userId, initAmount);
                cif = account != null ? account.getCif() : "";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new WSException(WSException.ERROR, e.getMessage());
        }

        return cif;
    }

    @WebMethod(operationName = "getBalance")
    public long getBalance(@WebParam(name = "cif") String cif) throws WSException {
        long balance = 0;
        boolean isAuthenticated = this.authenticate();
        if (!isAuthenticated) {
            logger.info("Authentication failed");
            throw new WSException(WSException.AUTHENTICATION_FAILED, "Authentication failed");
        } else {
            try {
                Account account = accountService.findAccountByCif(cif);
                if (account != null) {
                    balance = account.getBalance();
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new WSException(WSException.ERROR, e.getMessage());
            }
        }
        return balance;
    }
}
